package org.xxx.epms.admin.controller;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.xxx.epms.admin.dto.AdminDetailsDto;
import org.xxx.epms.admin.dto.AdminParam;
import org.xxx.epms.admin.models.Admin;
import org.xxx.epms.admin.models.AdminDetails;
import org.xxx.epms.admin.models.AdminPermission;
import org.xxx.epms.admin.service.AdminService;
import org.xxx.epms.common.api.CommonResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin")
@RefreshScope
public class AdminController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/getAdminDetails", method = RequestMethod.GET)
    public AdminDetailsDto getAdminDetails(@RequestParam("adminCode")  String adminCode){
        AdminDetails adminDetails = adminService.loadUserByUsername(adminCode);
        return new AdminDetailsDto(adminDetails);
    }

    @GetMapping("/getAdminPermissionList")
    public List<AdminPermission> getAdminPermissionList(){
        return adminService.getAdminPermissionListByAdminId(0);
    }

    //注册用户接口
    @RequestMapping(value = "/register",method = RequestMethod.POST )
    @ApiOperation(value = "注册用户", notes = "注册用户")
    public CommonResult<Admin> register(@Validated @RequestBody AdminParam adminParam){
        Admin admin = null;
        try {
            admin = adminService.register(adminParam);
        }catch (Exception e){
            return CommonResult.failed(e.getMessage());
        }
        if (admin == null) {
            return CommonResult.failed();
        }
        return CommonResult.success(admin);
    }

    //登录接口
    @RequestMapping(value = "/login",method = RequestMethod.POST )
    @ApiOperation(value = "登录", notes = "登录")
    public CommonResult login(@Validated @RequestBody AdminParam adminParam) {
        log.info("login param: " + adminParam.toString());
        String token = null;
        try{
            token = adminService.login(adminParam);
        }catch (Exception e){
            return CommonResult.failed(e.getMessage());
        }
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
//        log.info("login success, token: " + token);
        return CommonResult.success(tokenMap);
    }

    //获取用户信息接口
    @RequestMapping(value = "/info",method = RequestMethod.POST )
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    public CommonResult<Admin> getAdminInfoByAdminCode(@RequestBody AdminParam adminParam) {
        Admin admin = adminService.getAdminByAdminCode(adminParam.getAdminCode());
        if (admin == null) {
            return CommonResult.failed("用户不存在");
        }
        admin.setAdminPassword(null);
        return CommonResult.success(admin);
    }

    @GetMapping("/test")
    public CommonResult getUserConfigTest(){
        return CommonResult.success("success");
    }
}
